<?php

namespace App\Admin\Forms;

use App\Models\ProcessesArchive;
use App\Models\UserProcess;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;

class SetProcessesArchives extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        // 表单参数
        $processes_archives_ids = $input['processes_archives_ids'] ?? null;

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $user_process = UserProcess::query()->find($id);

        if (!$user_process) {
            return $this->response()->error('技师信息不存在');
        }

        $user_process->update(['processes_archives_ids' => $processes_archives_ids]);

        return $this->response()->success('技师信息修改成功');
    }

    public function form()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        $processes_archive = ProcessesArchive::where('status', DEFAULT_TRUE)->where('processes_type', PROCESSES_IN)->pluck('title', 'id');

        $this->listbox('processes_archives_ids')->options($processes_archive);
    }

    // 返回表单数据，如不需要可以删除此方法
    public function default()
    {
        return [
            'processes_archives_ids'         => '',
        ];
    }
}